#include "sound.h"

void hisr(void);
void lisr(void);

#pragma code high_vector=0x0008
void high_interrupt(void)
        {
                _asm GOTO hisr _endasm
        }
///////////////////////////////////
#pragma code low_vector=0x0018
void low_interrupt(void)
        {
                _asm GOTO lisr _endasm
        }
#pragma code
///////////////////////////////////

void main (void)
{
///////////////////////////////////////////////////////
//Setup for the selected external interupt source
///////////////////////////////////////////////////////
		//Configure TIMER0
		


        while(1)
        {
        }
}
void setup_sound(void)
{
		T0CONbits.T08BIT = 0;    		//16bit
		T0CONbits.T0CS = 0;              //internal clock
		T0CONbits.T0SE = 0;              //edge
		T0CONbits.PSA = 0;               //prescaler (0=assigned, 1=unassigned)
		T0CONbits.T0PS2 = 1;
		T0CONbits.T0PS1 = 0;
		T0CONbits.T0PS0 = 1;
		
		TRISA &= 0b11110111;            //pins as output
		TRISC &= 0b11111011;

		//Configure and Enable Interrupts
		
		INTCONbits.TMR0IF = 0;
		INTCONbits.TMR0IE = 1;          //Enable Interrupt for TMR0
		INTCON2bits.TMR0IP = 0;			//Low Priorty
		RCONbits.IPEN = 1;              //enable priorities
		INTCONbits.PEIE = 1;           	//enable perepheral
		INTCONbits.GIE = 1;             //enable global
		T0CONbits.TMR0ON = 1;
}

//Requres a time_scaler char between 0-255 (0-100%)
void lisr_tmr0_overflow(unsigned char time_scaler)
{
        //Minor Delay (Beep)
        while(beep_counter <= MINOR_LENGTH)
        {
			//Toggle
	        if(PORTCbits.RC2 == 0){
				PORTCbits.RC2 = 1;
			}
			else{
				PORTCbits.RC2 = 0;
			}
				beep_counter++;
				
			//Change this to change frequency of beep
			Delay10TCYx(10);
        }
   
		//Major Delay (Gap)
		TMR0L = 0x00; 
		TMR0H = time_scaler;   
        beep_counter = 0;
}

#pragma interruptlow lisr
void lisr(void)
{	
	if(INTCONbits.TMR0IF == 1)
	{
		INTCONbits.TMR0IF = 0;
		lisr_tmr0_overflow();
	}
	
}
#pragma interrupt hisr
void hisr(void)
{
}

